package view;

import java.rmi.RemoteException;

import javax.swing.JFrame;
import javax.swing.JTextField;


import controller.FirewallSession;

import server.FirewallException;
import server.NotExpertException;
import server.ReadWriteException;

public class RulesetNewFrame extends AbstractTextFieldFrame
{
	/**
	 * Serial version ID - for serialization
	 */
	private static final long serialVersionUID = 8132796840501212792L;
	
	/**
	 * The text box which displayed.
	 */
	private static JTextField textFiled = null;
	
	/**
	 * server controller
	 */
	public FirewallSession UI;
	
	/**
	 * C'tor
	 * Define the frame
	 */
	public RulesetNewFrame(FirewallSession ui)
	{
	    super("Ruleset", "Enter the name of the new ruleset:");
	    UI = ui;
	    textFiled = new JTextField(Settings.ruleset);
	    addDataField(textFiled);
	}

	/**
	 * Action performed by finish button press:
	 * save the relevant ruleset from two tables 
	 */
	protected void finishAction()
	{
		String newRuleset = textFiled.getText();
		
		try {
			UI.createRuleSet(newRuleset);
			Settings.ruleset = newRuleset;
			
			/* Build the appropriate table */
			switch(Settings.userMode)
			{
			case STANDART:
				RulesStandartFrame.currentRulesFrame.isCurrentRuleSet(false);
				RulesStandartTable.currentInRulesTable.build();
				RulesStandartTable.currentOutRulesTable.build();
				break;
				
			case ADVANCED:
				RulesAdvancedFrame.currentRulesFrame.isCurrentRuleSet(false);
				RulesAdvancedTable.currentInRulesTable.build();
				RulesAdvancedTable.currentOutRulesTable.build();
				break;
			}
			
			dispose();
		
		} catch (RemoteException e) {
			ErrorFrame.AddError("Failed to create the ruleset:" + e.getMessage(), new JFrame());
		} catch (FirewallException e) {
			ErrorFrame.AddError("Failed to create the ruleset:" + e.getMessage(), new JFrame());
		} catch (ReadWriteException e) {
			ErrorFrame.AddError("Failed to create the ruleset:" + e.getMessage(), new JFrame());
		} catch (NotExpertException e) {
			ErrorFrame.AddError("Failed to create the ruleset:" + e.getMessage(), new JFrame());
		}
	}
}
